import React, { useState } from 'react';
import SelfSelect from './SelfSelect';
import { services } from '../../PublicServices/selectOptions';

let optionsCache = {};

const Com = props => {
  const { cache = false, serviceKey, valueKey, textKey } = props;
  const renderOption = Option => node => (
    <Option key={node[valueKey]} value={node[valueKey]}>
      {node[textKey]}
    </Option>
  );

  const [options, setOptions] = useState(cache ? optionsCache[serviceKey] : []);

  const getData = async () => {
    const resp = await services[serviceKey]();
    optionsCache[serviceKey] = resp.Data;
    setOptions(resp.Data);
  };
  return (
    <SelfSelect
      renderOption={renderOption}
      options={options}
      getData={getData}
      {...props}
    />
  );
};

export default Com;
